;
$(function ($) {
    /*顶部下拉框js事件*/
    $('.frame-top-body .top-right .dropdown-link').each(function () {
        $(this).parent('.dropdown').mouseover(function () {
            $(this).find('.action-box').show();
        }).mouseout(function () {
            $(this).find('.action-box').hide();
        });
    });
    $('.frame-top-body .top-right .all-actions-list .all-actions-title i.off').off('click').on('click',function () {
        $(this).parents('.all-actions-list').slideUp('slow');
    });
    $('#all-actions-btn').on('click',function () {
        $(this).siblings('.all-actions-list').slideToggle('slow','swing');
    })
    /*左侧菜单js事件*/
    $('.frame-left-body .nav-one a[data-target]').each(function () {
        $(this).off('click').on('click',function () {
            var target = $(this).data('target');
            if(target == 'parent'){
                var elem = $(this).parent();
            }else{
                var elem = $(this);
            }
            if(!elem.hasClass('hover')){
                elem.addClass('hover');
                elem.siblings().removeClass('hover')
            }else{
                if(elem.hasClass('has-children')){
                    elem.removeClass('hover')
                }
            }
        })
    });

})
/**
 * 导航选中
 * @type {{focusByUrl}}
 */
var navFocus = (function () {
    var focusByUrl = function(route_name){

        var current_elems = $("#left-menu").find('a[data-url="' + route_name + '"]');

        if(!current_elems.length){
            current_elems = $("#left-menu").find('a[data-level="2"]');
        }
        var current_elem = $(current_elems[current_elems.length-1]);
        var current_level = $(current_elem).data('level');
        var target = $(current_elem).data('target');
        if(target == 'parent'){
            var elem = $(current_elem).parent();
        }else{
            var elem = $(current_elem);
        }
        $(elem).addClass('hover');
        switch (current_level){
			case 1:
                $('#left-menu').css('width','90px');
				break;
			case 2:
                $(current_elem).parents('li').addClass('hover');
				break;
			default:
                $(current_elem).parents('li').addClass('hover');
                $(current_elem).parents('div.nav-three').show();
		}
    };
    return {
        focusByUrl:focusByUrl
    };
})();

function logout(url) {
    layer.confirm('确定要退出?', {icon: 3, title:'提示'}, function(index){
        window.location.href = url;
        layer.close(index);
    });
}

function randomPassword(size) {
    var seedUppercase = new Array(
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'
    );
    var seedLowercase = new Array(
        'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z'
    );
    var seedDigit = new Array('2','3','4','5','6','7','8','9')
    var seedSymbol = new Array('?','%','$','*','@',')','(');
    var seed = seedUppercase.concat(seedLowercase,seedDigit,seedSymbol);

    var seedLength = seed.length;//数组长度
    var createPassword = '';

    for (var i=0;i<size;i++) {
        var j = Math.floor(Math.random()*seedLength);
        createPassword += seed[j];
    }
    return createPassword;
}

/**
 * @title 生成随机密码
 * @param obj
 * @param size
 */
function randVal(obj,size) {
    var randomVal = randomPassword(size);
    var target = $(obj).data('target');
    $('input[name="'+target+'"]').val(randomVal);
    $('input[name="'+target+'"]').attr('type','text');
    $('input[name="confirm_'+target+'"]').val(randomVal);
    $('input[name="confirm_'+target+'"]').attr('type','text');
}

function randNumber(obj,size) {
    var seedUppercase = new Array(
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'
    );
    // var seedLowercase = new Array(
    //     'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z'
    // );
    var seedDigit = new Array('0','1','2','3','5','6','7','8','9')

    var seedLength = seedDigit.length;//数组长度
    var randomVal = '';

    var prefixLength = seedUppercase.length;

    for (var i=0;i<2;i++) {
        var index = Math.floor(Math.random()*prefixLength);
        randomVal += seedUppercase[index];
    }
    for (var i=0;i<size;i++) {
        var j = Math.floor(Math.random()*seedLength);
        randomVal += seedDigit[j];
    }
    var target = $(obj).data('target');
    $('input[name="'+target+'"]').val(randomVal);
}

function randMobile(obj) {
    var seedDigit = new Array('0','1','2','3','5','6','7','8','9');
    var prefixDigit = new Array('3','5','6','7','8','9');

    var seedLength = seedDigit.length;//数组长度
    var prefixLength = prefixDigit.length;//数组长度
    var randomVal = '1';

    for (var i=0;i<2;i++) {
        var index = Math.floor(Math.random()*prefixLength);
        randomVal += prefixDigit[index];
    }
    for (var i=0;i<8;i++) {
        var j = Math.floor(Math.random()*seedLength);
        randomVal += seedDigit[j];
    }
    var target = $(obj).data('target');
    $('input[name="'+target+'"]').val(randomVal);
}


function exportData(obj) {

    var params = $(obj).data("params");

    if(params==undefined||params == ''){
        layer.msg('确实必要参数');
        return false;
    }
    params = (typeof params == "string")? params.parseJSON():params;
    var url = $(obj).attr('href')||$(obj).attr('url')||params.url;


    if(params != undefined && params != ''){

        var data = $('.table input'+params.targetData).serialize();
        if(data!=undefined && data!=''){
            if(url.indexOf("?") >= 1){
                url = url+"&"+data;
            }else{
                url = url+"?"+data;
            }
        }
        window.location.href = url;
    }
}

function searchExport(obj) {
    var data = $("form[name='search']").serialize();
    var dataArray = data.split('&');
    var newData = []
    for (var index in dataArray){
        var item = dataArray[index];
        if(item.length > (item.indexOf('=')+1)){
            if(item.split('=')[1] !=-100){
                newData.push(item);
            }
        }
    }

    var params = $(obj).data("params");
    if(params==undefined||params == ''){
        layer.msg('确实必要参数');
        return false;
    }
    params = (typeof params == "string")? params.parseJSON():params;
    var url = $(obj).attr('href')||$(obj).attr('url')||params.url;

    if(newData.length>=1){
        if(url.indexOf("?") >= 1){
            url = url+"&"+newData.join('&');
        }else{
            url = url+"?"+newData.join('&');
        }
    }
    window.location = url;
    return true;
}